home *** CD-ROM | disk | FTP | other *** search
/ PC Open 92 / PC Open 92 CD 1.bin / internet / AmphetaDesk / lib / AmphetaDesk / Utilities.pm < prev    next >
Encoding:
Perl POD Document  |  2002-10-23  |  9.3 KB  |  208 lines

  1. package AmphetaDesk::Utilities;
  2. ###############################################################################
  3. # AmphetaDesk                                           (c) 2000-2002 Disobey #
  4. # morbus@disobey.com                      http://www.disobey.com/amphetadesk/ #
  5. ###############################################################################
  6. # ABOUT THIS PACKAGE:                                                         #
  7. #   This package contains all the minor utilities needed through out Ampheta, #
  8. #   like logging, newline removable, HTML removal, and so on. None of these   #
  9. #   routines require knowledge of the AmphetaDesk SETTINGS, so use freely.    #
  10. #                                                                             #
  11. # LIST OF ROUTINES BELOW:                                                     #
  12. #   encode_to_dec - encodes certain characters into decimal equivalents.      #
  13. #   get_response - returns the currently saved response array.                #
  14. #   note and error - send a message to our logfile.                           #
  15. #   set_response - sets the response to user action in an array.              #
  16. #   strip_newlines_and_tabs - strips all newlines and tabs from incoming.     #
  17. ###############################################################################
  18. #      "Use freely? Bah! Wait until the RIAA gets their hands on it."         #
  19. ###############################################################################
  20.  
  21. use strict; $|++;
  22. use URI::Escape;
  23. require Exporter;
  24. use vars qw( @ISA @EXPORT );
  25. @ISA = qw( Exporter );
  26. @EXPORT = qw( encode_to_dec error get_response note
  27.               set_response strip_newlines_and_tabs );
  28.  
  29. # where we store responses that should be shown
  30. # to the user based on their requested action.
  31. # see the get_ and set_response routines.
  32. my @RESPONSES;
  33.  
  34. ###############################################################################
  35. # encode_to_dec - encodes certain characters into decimal equivalents.        #
  36. ###############################################################################
  37. # USAGE:                                                                      #
  38. #    $modified = encode_to_dec( $data );                                      #
  39. #                                                                             #
  40. # NOTES:                                                                      #
  41. #    Used to encode non-alphanumerics to decimal equivalents (like %20).      #
  42. #                                                                             #
  43. # RETURNS:                                                                    #
  44. #    $modified; the modified data, with non-alphanumerics encoded.            #
  45. ###############################################################################
  46.  
  47. sub encode_to_dec {
  48.    my ($toencode) = @_; # what's a toe ncode? horrible hangnail?
  49.    $toencode = uri_escape($toencode, "^a-zA-Z0-9_.-");
  50.    return $toencode; # what sort of song are we singing to enc?
  51. }
  52.  
  53. ###############################################################################
  54. # get_response - returns the currently saved response array.                  #
  55. # set_response - sets the response to user action in an array.                #
  56. ###############################################################################
  57. # USAGE:                                                                      #
  58. #    my @answers = get_response( );                                           #
  59. #    set_response( "You've successfully added a channel!" );                  #
  60. #                                                                             #
  61. # NOTES:                                                                      #
  62. #    Returns the currently saved response, which is used for displaying       #
  63. #    diagnostic messages in the browser window (either in normal template     #
  64. #    pages, or in a javascript popup window. set_response will save a new     #
  65. #    response. get_response will remove the saved response(s) once retrieved. #
  66. #                                                                             #
  67. #    One way of handling the response is:                                     #
  68. #        my $response = join("<br />", get_response());                       #
  69. #                                                                             #
  70. # RETURNS:                                                                    #
  71. #    $value; the value of the passed or set.                                  #
  72. #    undef; if the setting doesn't exist or isn't defined.                    #
  73. ###############################################################################
  74.  
  75. sub get_response {
  76.    my @responses = @RESPONSES;
  77.    undef @RESPONSES; # read once.
  78.    return @responses;
  79. }
  80.  
  81. sub set_response {
  82.    my ($response) = @_;
  83.    push (@RESPONSES, $response);
  84. }
  85.  
  86. ###############################################################################
  87. # note and error - send a message to our logfile.                             #
  88. ###############################################################################
  89. # USAGE:                                                                      #
  90. #   note("This is a logged line. Yup.");          sends to logfile.           #
  91. #   note("This is a logged line. Yup.", 1);     sends to gui window also.     #
  92. #   note("This is added to @RESPONSES", 1, 1); add to our responses array.    #
  93. #   error("This is an error!");                  die after logging.           #
  94. #                                                                             #
  95. # NOTES:                                                                      #
  96. #   You may use note to write a note to the gui window and LOG, and return    #
  97. #   happily. Whatever happens in the GUI portion is controlled by those       #
  98. #   libraries. error reaches into note, and exits the script when finished.   #
  99. #                                                                             #
  100. # RETURNS:                                                                    #
  101. #   1; if the log was successfully written to.                                #
  102. ###############################################################################
  103.  
  104. sub note {
  105.  
  106.    my ($message, $gui, $response) = @_;
  107.  
  108.    # what time is it, kenneth?
  109.    my ($sec, $min, $hour) = localtime;
  110.    $sec = sprintf "%02.0d", $sec;
  111.    $min = sprintf "%02.0d", $min;
  112.    $hour = sprintf "%02.0d", $hour;
  113.  
  114.    # print the entry to our log file.
  115.    print LOG "[$hour:$min:$sec] $message\n";
  116.  
  117.    # and save the message in our response log
  118.    # if the message is also being sent to the gui.
  119.    set_response($message) if $response;
  120.  
  121.    # if we've been told to pass it to our GUI, do so.
  122.    # see cookbook 12.13. there are probably better
  123.    # and smarter ways to do this, but I'm fed up.
  124.    {
  125.       no strict 'refs'; my $os;
  126.       $os = "MacOSX"  if $^O =~ /darwin/;
  127.       $os = "MacOS"   if $^O =~ /Mac/;
  128.       $os = "Windows" if $^O =~ /Win/;
  129.       $os = "Linux" unless defined $os;
  130.       my $packname = "AmphetaDesk::OS::";
  131.       my $funcname = "::gui_note";
  132.       &{ $packname. $os . $funcname }($message) if $gui;
  133.    }
  134.  
  135.    return 1;
  136.  
  137. }
  138.  
  139. sub error {
  140.  
  141.    my ($message) = @_;
  142.  
  143.    # send everywhere.
  144.    note($message);
  145.  
  146.    # if we've been told to pass it to our GUI, do so.
  147.    # see cookbook 12.13. there are probably better
  148.    # and smarter ways to do this, but I'm fed up.
  149.    { 
  150.       no strict 'refs'; my $os;
  151.       $os = "MacOS"   if $^O =~ /Mac/;
  152.       $os = "Windows" if $^O =~ /Win/;
  153.       $os = "Linux" unless defined $os;
  154.       my $packname = "AmphetaDesk::OS::";
  155.       my $funcname = "::gui_note";
  156.       &{ $packname. $os . $funcname }($message);
  157.    }
  158.  
  159.    # we sleep for 10 seconds so that the 
  160.    # error message is seen by someone.
  161.    sleep 10;
  162.  
  163.    exit;
  164.  
  165. }
  166.  
  167. ###############################################################################
  168. # strip_newlines_and_tabs - strips all newlines and tabs from incoming.       #
  169. ###############################################################################
  170. # USAGE:                                                                      #
  171. #    $modified = strip_newlines_and_tabs( $data );                            #
  172. #                                                                             #
  173. # NOTES:                                                                      #
  174. #    This routine removes newlines and tabs from the passed data. It can      #
  175. #    dip into arrays, single level hashes, and normal variables. It replaces  #
  176. #    all newlines and tabs with a single space character.                     #
  177. #                                                                             #
  178. # RETURNS:                                                                    #
  179. #    $modified; the modified data, sans newlines and tabs.                    #
  180. ###############################################################################
  181.  
  182. sub strip_newlines_and_tabs {
  183.  
  184.    my ($data) = @_;
  185.  
  186.    # depending on our data type,
  187.    # process it differently.
  188.    if (ref($data) eq "HASH") {
  189.       foreach ( keys %{ $data } ) {
  190.          next if not defined( $data->{$_} );
  191.          $data->{$_} =~ s/\n|\r|\f|\t/ /g if defined $data;
  192.       }
  193.    }
  194.    elsif (ref($data) eq "ARRAY") {
  195.       foreach ( @ { $data } ) {
  196.          next if not defined( $data->[$_] );
  197.          $data->[$_] =~ s/\n|\r|\f|\t/ /g  if defined $data;
  198.       }
  199.    }
  200.    else {
  201.       $data =~ s/\n|\r|\f|\t/ /g if defined $data;
  202.    }
  203.  
  204.    return $data;
  205.  
  206. }
  207.  
  208. 1;